package br.ufmg.ubicomp.droidguide.eventservice;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import br.ufmg.ubicomp.droidguide.common.DroidGuideEntity;
import br.ufmg.ubicomp.droidguide.eventservice.enums.ParameterType;
import br.ufmg.ubicomp.droidguide.eventservice.enums.ServiceType;

/**
 * service class that contains a representation of remote services that 
 * can be accessed by the device.
 * @author wpjr
 *
 */

public class DroidGuideRemoteService extends DroidGuideEntity implements Comparable<DroidGuideRemoteService> {

	private String description;

	private Set<ServiceType> types;
	private String state;
	private String data;
	private Calendar dateTime;
	private boolean marked;	

	private Map<String, DroidGuideServiceParameter> parameters;
	
	public DroidGuideRemoteService() {
		types = new HashSet<ServiceType>();
		parameters = new HashMap<String, DroidGuideServiceParameter>();
	}

	public void addParameter(String name, String value, ParameterType type)
	{
		DroidGuideServiceParameter param = new DroidGuideServiceParameter(name, value, type);
		addParameter(param);
	}
	
	public void addParameter(DroidGuideServiceParameter param)
	{
		parameters.put(param.getName(), param);
	}
	
	public Collection<DroidGuideServiceParameter> getParameters() {
		return parameters.values();
	}

	public Collection<DroidGuideServiceParameter> getRequestParameters() {
		Collection<DroidGuideServiceParameter> list = new ArrayList<DroidGuideServiceParameter>();
		for (DroidGuideServiceParameter param: parameters.values())
		{
			if (param.getType().equals(ParameterType.REQUEST))
			{
				list.add(param);
			}
		}
		return list;
	}
	
	public DroidGuideServiceParameter getParameterByName(String name) {
		return parameters.get(name);
	}


	public boolean isMarked() {
		return marked;
	}

	public void setMarked(boolean marked) {
		this.marked = marked;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Calendar getDateTime() {
		return dateTime;
	}

	public void setDateTime(Calendar dateTime) {
		this.dateTime = dateTime;
	}

	public void setTypes(Set<ServiceType> types) {
		this.types = types;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Set<ServiceType> getTypes() {
		return types;
	}

	public void addType(ServiceType type) {
		types.add(type);
	}

	public int compareTo(DroidGuideRemoteService o) {
		if (getKey() != null && o.getKey() != null)
		{
			return getKey().compareTo(o.getKey());
		}
		return toString().compareTo(o.toString());
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof DroidGuideRemoteService)
		{
			DroidGuideRemoteService other = (DroidGuideRemoteService) obj;
			if (getKey() != null && other.getKey() != null)
			{
				return getKey().equals(other.getKey());
			}
		}
		return false;
	}
}
